--------------------------------------------------------------------------------
-- Dan's LÖVE Spunk
-- http://sites.google.com/site/danslovespunk/
-- Random Patchwork
--------------------------------------------------------------------------------

-- Amount of patches
local xPatches = 10
local yPatches = 10
-- Sizes, updated in regenPatchwork
local xSize = 1
local ySize = 1

-- Table to hold all the colour values
local patchworkVals = {}

--------------------------------------------------------------------------------

-- Generate patches on load
function love.load()
    chooseStartingSize()
    regenPatchwork()
end

function chooseStartingSize()
    local xScreen = love.graphics.getWidth()
    local yScreen = love.graphics.getHeight()
    if (xScreen % 6) == 0  then xPatches = 6  end
    if (xScreen % 7) == 0  then xPatches = 7  end
    if (xScreen % 8) == 0  then xPatches = 8  end
    if (xScreen % 9) == 0  then xPatches = 9  end
    if (xScreen % 10) == 0 then xPatches = 10 end
    if (yScreen % 6) == 0  then yPatches = 6  end
    if (yScreen % 7) == 0  then yPatches = 7  end
    if (yScreen % 8) == 0  then yPatches = 8  end
    if (yScreen % 9) == 0  then yPatches = 9  end
    if (yScreen % 10) == 0 then yPatches = 10 end
end

function love.draw()
 -- Window title with Frames Per Second
    local win = 'Random Patchwork'
    local fps = ' [FPS:'..love.timer.getFPS()..']'
    love.graphics.setCaption(win..fps)
    local totalPatches = xPatches * yPatches
 -- Draw the patches
    for i=1,totalPatches do
        love.graphics.setColor(patchworkVals[i].rgb)
        love.graphics.rectangle('fill', patchworkVals[i].xpos, patchworkVals[i].ypos, xSize, ySize)
    end
end

-- Alias any input to regenerate the patchwork
function love.mousepressed(x, y, button)         regenPatchwork() end
function love.mousereleased(x, y, button)        regenPatchwork() end
function love.joystickpressed(joystick, button)  regenPatchwork() end
function love.joystickreleased(joystick, button) regenPatchwork() end

-- Pressing + increases the amount of patches - decreases
function love.keypressed(key, unicode)
 -- Capture + and - keypresses
    if unicode == 43 then
        xPatches = xPatches + 1
        yPatches = yPatches + 1
    elseif unicode == 45 then
        xPatches = xPatches - 1
        yPatches = yPatches - 1
    end
 -- Error check x & y values
    if xPatches > love.graphics.getWidth() then
        xPatches = love.graphics.getWidth()
    end
    if xPatches < 1 then
        xPatches = 1
    end
    if yPatches > love.graphics.getHeight() then
        yPatches = love.graphics.getHeight()
    end
    if yPatches < 1 then
        yPatches = 1
    end
 -- Regen patches
    regenPatchwork()
end

-- Regenerate the colour value for all the squares
function regenPatchwork()
    local xScreen = love.graphics.getWidth()
    local yScreen = love.graphics.getHeight()
    xSize = (xScreen/xPatches)
    ySize = (yScreen/yPatches)
 -- Reset
    patchworkVals = {}
 -- Seed random colours
    math.randomseed(os.time())
 -- XY loop to add patch values
    local pvCount = 0
    for y=1,yPatches do
        for x=1,xPatches do
            pvCount = pvCount + 1
            patchworkVals[pvCount] = {xpos=((x-1)*xSize), ypos=((y-1)*ySize), rgb={randComp(), randComp(), randComp()}}
        end
    end
end

-- Generate a random colour componant
function randComp()
    return math.random(0,255)
end

--------------------------------------------------------------------------------
